UnitDoodads = {}
local mt = {}
UnitDoodads.__index = mt
setmetatable(UnitDoodads, mt)



function UnitDoodads.new(path)
	local self = setmetatable({}, UnitDoodads)
	local file = io.open( path, 'rb' )
	self.file_string = file:read '*a'
	file:close()
	return self
end

function mt:is_unit_id( id )
	--	todo：补偿判断
	-- if id == "sloc"or id == "uDNR" or id == "bDNR" then
	-- 	return true
	-- else
	-- 	return false
	-- end
	--	因为地图不存在预设物品
	return true
end

function mt:load()
	--------------解析头部标志------------------
	local binary_reader = Binary_reader.new(self.file_string)

	--读4个字节固定长度的字符串
	local flag = binary_reader:read 'c4'
	if flag ~= 'W3do' then
		log.error('解析unit doo文件：该文件格式错误')
		return nil
	end
	--读32位的 一个有符号 long	uint32_t
	self.version = binary_reader:read 'l'
	if self.version ~= 7 and self.version ~= 8 then
		log.error('解析doo文件：doo版本错误，只支持1.24以上:'..self.version)
	end
	self.subversion = binary_reader:read 'l'
	if self.subversion ~= 9 and self.subversion ~= 11 then
		log.error('解析doo文件：doo子版序号错误:'..self.subversion)
	end

	--------------解析id内容-------------------
	local count = binary_reader:read 'l'
	self.units = {}
	self.items = {}
	for i = 1, count do
		local obj = {}
		obj.id = binary_reader:read 'c4'
		--样式
		obj.variation = binary_reader:read 'l'
		obj.position = {
			binary_reader:read 'f',
			binary_reader:read 'f',
			binary_reader:read 'f',
		}
		print(1, obj.id, obj.variation, obj.position[1], obj.position[2], obj.position[3])
		--弧度制
		obj.angle = binary_reader:read 'f'

		obj.scale = {
			binary_reader:read 'f',
			binary_reader:read 'f',
			binary_reader:read 'f',
		}

		obj.flags = binary_reader:read 'b'
		
		obj.player = binary_reader:read 'l'


		obj.unknown1 = binary_reader:read 'b'
		obj.unknown2 = binary_reader:read 'b'

		obj.life = binary_reader:read 'l'
		obj.mana = binary_reader:read 'l'
		
		if self.version >= 8 then
			obj.item_table_pointer = binary_reader:read 'l'
		end

		local c = binary_reader:read 'l'
		obj.item_sets = {}
		for j = 1, c do
			obj.item_sets[j] = {}
			local n = binary_reader:read 'l'
			for k = 1, n do
				obj.item_sets[j][#obj.item_sets[j]+1] = {
					id = binary_reader:read 'c4',
					chance = binary_reader:read 'l',
				}
			end
		end

		obj.gold = binary_reader:read 'l'
		obj.target_acquisition = binary_reader:read 'f'
		obj.level = binary_reader:read 'l'

		if self.version >= 8 then
			obj.strength = binary_reader:read 'l'
			obj.agility = binary_reader:read 'l'
			obj.intelligence = binary_reader:read 'l'
		end

		obj.items = {}
		for j = 1, binary_reader:read 'l' do
			obj.items[j] = {
				slot = binary_reader:read 'l',
				id = binary_reader:read 'c4',
			}
		end

		obj.abilities = {}
		for j = 1, binary_reader:read 'l' do
			obj.abilities[j] = {
				id = binary_reader:read 'c4',
				autocast = binary_reader:read 'l',
				level = binary_reader:read 'l',
			}
		end

		obj.random_type = binary_reader:read 'l'
		if obj.random_type == 0 then
			obj.random = {
				binary_reader:read 'b',
				binary_reader:read 'b',
				binary_reader:read 'b',
				binary_reader:read 'b',
			}
		elseif obj.random_type == 1 then
			obj.random = {
				binary_reader:read 'b',
				binary_reader:read 'b',
				binary_reader:read 'b',
				binary_reader:read 'b',
				binary_reader:read 'b',
				binary_reader:read 'b',
				binary_reader:read 'b',
				binary_reader:read 'b',
			}
		elseif obj.random_type == 2 then
			local n = binary_reader:read 'l'
			obj.random = {}
			for j = 1, n do
				for k = 1, 8 do
					obj.random[#obj.random+1] = binary_reader:read 'b'
				end
			end
		end

		obj.custom_color = binary_reader:read 'l'
		obj.waygate = binary_reader:read 'l'
		obj.creation_number = binary_reader:read 'l'

		if self:is_unit_id(obj.id) then
			self.units[#self.units+1] = obj
		else
			self.items[#self.items+1] = obj
		end

	end

	return true
end

function mt:save()

	local binary_writer = Binary_writer.new()
	

	return binary_writer:file_write()
end